//
//  HLBaseTabBarController.m
//  HouLu
//
//  Created by WANGGNIT on 2019/1/7.
//  Copyright © 2019 曦岸. All rights reserved.
//

#import "HLBaseTabBarController.h"
#import "AppDelegate.h"
#import "ZYHomeVC.h"
#import "ZYChatVC.h"
#import "ZYOrderVC.h"
#import "ZYMineVC.h"

@interface HLBaseTabBarController () 
{
    BOOL isAnimating;
}
@end

@implementation HLBaseTabBarController

#pragma mark - 第一次使用当前类的时候对设置UITabBarItem的主题

+ (void)initialize{
    
    UITabBarItem *tabBarItem = [UITabBarItem appearanceWhenContainedInInstancesOfClasses:@[self]];

    NSMutableDictionary *dictNormal = [NSMutableDictionary dictionary];
    dictNormal[NSForegroundColorAttributeName] = HLBlackColor;
    
    dictNormal[NSFontAttributeName] = [UIFont systemFontOfSize:11];

    NSMutableDictionary *dictSelected = [NSMutableDictionary dictionary];
    dictSelected[NSForegroundColorAttributeName] = HLRedColor;
    dictSelected[NSFontAttributeName] = [UIFont systemFontOfSize:11];

    [tabBarItem setTitleTextAttributes:dictNormal forState:UIControlStateNormal];
    [tabBarItem setTitleTextAttributes:dictSelected forState:UIControlStateSelected];
    
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self setUpAllChildVc];
    
    [[UITabBar appearance] setTranslucent:NO];
    
    //创建自己的tabbar，然后用kvc将自己的tabbar和系统的tabBar替换下
    
    _myTab = [[HLHomeTabbar alloc] init];
    
    _myTab.isSelect = NO;
    
    //kvc实质是修改了系统的_tabBar
    [self setValue:_myTab forKeyPath:@"tabBar"];
    
    CGRect rect = CGRectMake(0,0, SCREEN_WIDTH ,1);
    
    UIGraphicsBeginImageContext(rect.size);
    
    CGContextRef context =UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
    
    CGContextFillRect(context, rect);
    
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    [_myTab setBackgroundImage:img];
    
    [_myTab setShadowImage:img];
    
    _myTab.backgroundColor = [UIColor whiteColor];
    
    [[UITabBar appearance] setShadowImage:[UIImage new]];
    
    [[UITabBar appearance] setBackgroundImage:[[UIImage alloc]init]];
    
    [self dropShadowWithOffset:CGSizeMake(0, -0.5)
                        radius:1
                         color:[UIColor grayColor]
                       opacity:0.3];
    
    [self.view bringSubviewToFront:_myTab];
    
    
    
    [self initSetting];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBar.hidden = YES;
}

- (void)dropShadowWithOffset:(CGSize)offset
                      radius:(CGFloat)radius
                       color:(UIColor *)color
                     opacity:(CGFloat)opacity {
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, self.tabBar.bounds);
    _myTab.layer.shadowPath = path;
    CGPathCloseSubpath(path);
    CGPathRelease(path);
    
    _myTab.layer.shadowColor = color.CGColor;
    _myTab.layer.shadowOffset = offset;
    _myTab.layer.shadowRadius = radius;
    _myTab.layer.shadowOpacity = opacity;
    _myTab.clipsToBounds = NO;
    
}

#pragma mark - 初始化tabBar上除了中间按钮之外所有的按钮

-(void)initSetting{
    
   
}

- (void)setUpAllChildVc{
    
    ZYHomeVC * HomeVC = [[ZYHomeVC alloc] init];
    [self setUpOneChildVcWithVc:HomeVC Image:@"homeTabbarPic" selectedImage:@"homeTabS" title:@"首页"];

    ZYChatVC * FishVC = [[ZYChatVC alloc] init];
    [self setUpOneChildVcWithVc:FishVC Image:@"chatTabbarPic" selectedImage:@"friendS" title:@"消息"];

    ZYOrderVC * MessageVC = [[ZYOrderVC alloc] init];
    [self setUpOneChildVcWithVc:MessageVC Image:@"orderTabbarPic" selectedImage:@"newsS" title:@"订单"];

    ZYMineVC * MineVC =[[ZYMineVC alloc]init];
    [self setUpOneChildVcWithVc:MineVC Image:@"mineTabbarPic" selectedImage:@"mineS" title:@"我的"];
}

#pragma mark - 初始化设置tabBar上面单个按钮的方法

/**
 *  @author li bo, 16/05/10
 *
 *  设置单个tabBarButton
 *
 *  @param Vc            每一个按钮对应的控制器
 *  @param image         每一个按钮对应的普通状态下图片
 *  @param selectedImage 每一个按钮对应的选中状态下的图片
 *  @param title         每一个按钮对应的标题
 */
- (void)setUpOneChildVcWithVc:(UIViewController *)Vc Image:(NSString *)image selectedImage:(NSString *)selectedImage title:(NSString *)title{
    
    RTContainerNavigationController *navVc = [[RTContainerNavigationController alloc] initWithRootViewController:Vc];
    //HLNavigationController * navVc = [[HLNavigationController  alloc] initWithRootViewController:Vc];
    
    UIImage *myImage = [UIImage imageNamed:image];
    
    myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    //tabBarItem，是系统提供模型，专门负责tabbar上按钮的文字以及图片展示
    Vc.tabBarItem.image = myImage;
    
    UIImage * mySelectedImage = [UIImage imageNamed:selectedImage];
    
    mySelectedImage = [mySelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    Vc.tabBarItem.selectedImage = mySelectedImage;
    
    Vc.tabBarItem.title = title;
    
    [Vc.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, 0)];
   
    [self addChildViewController:navVc];
    
}

- (BOOL)prefersStatusBarHidden
{
    return self.selectedViewController.prefersStatusBarHidden;
}

- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation
{
    return self.selectedViewController.preferredStatusBarUpdateAnimation;
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return self.selectedViewController.preferredStatusBarStyle;
}

@end
